#!/bin/bash

# $Date: 2009-04-08 13:25:39 +0300 $

shopt -s nullglob

LIB_FOLDER="${HOME}/src/libmacrame"
DOC_FOLDER="${LIB_FOLDER}/doc"
SRC_FOLDER="src"

# Make sure you point to your jsdoc-toolkit installation folder
# I have jsdoc-toolkit 2.1 + fixes for issues #193 and #198

JSDOC_FOLDER="${HOME}/src/jsdoc-toolkit-latest"
JSDOC_JAR="${JSDOC_FOLDER}/jsrun.jar"
JSDOC_JS="${JSDOC_FOLDER}/app/run.js"
JSDOC_TEMPLATE="${JSDOC_FOLDER}/templates/jsdoc"

if [[ ! -d "$LIB_FOLDER" ]]
then
  echo "The library folder does not exist: $LIB_FOLDER"
	exit 1
fi

if [[ ! -d "${LIB_FOLDER}/${SRC_FOLDER}" ]]
then
  echo "The library source folder does not exist: ${LIB_FOLDER}/${SRC_FOLDER}"
  exit 1
fi

if [[ ! -d "$JSDOC_FOLDER" ]]
then
	echo "The JSDoc folder does not exist: $JSDOC_FOLDER"
	exit 1
fi

if [[ ! -f "$JSDOC_JAR" ]]
then
  echo "The JSDoc-Toolkit .jar file does not exist!"
  exit 1
fi

if [[ ! -f "$JSDOC_JS" ]]
then
  echo "The JSDoc-Toolkit run.js file does not exist!"
  exit 1
fi

if [[ ! -d "$JSDOC_TEMPLATE" ]]
then
  echo "The JSDoc-Toolkit template folder does not exist: $JSDOC_TEMPLATE"
  exit 1
fi

# Simple cleanup
if [[ -d "$DOC_FOLDER" ]]
then
  cd  "$DOC_FOLDER"
  rm *html *js *css

  if [[ -d symbols ]]
  then
    cd symbols
    rm *html *js *css

    if [[ -d src ]]
    then
      cd src
      rm *html *js *css
    fi
  fi
fi

cd "$LIB_FOLDER"

java -jar "$JSDOC_JAR" "$JSDOC_JS" -r=10 -a -p -t=${JSDOC_TEMPLATE} \
-d=${DOC_FOLDER} "$SRC_FOLDER"

echo "It took $SECONDS seconds to generate the documentation."

